package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;

import com.collect.community.R;
import com.csoa.community.bean.LocalSpeciesBean;
import com.csoa.community.bean.WZMLInfoBean;
import com.csoa.community.db.LocalSpeciesInfoDB;
import com.csoa.community.db.WZMLInfoDB;
import com.csoa.community.db.YFInfoDB;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.AndroidUtils;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.CommonDialogList;
import com.csoa.community.widget.CommonDialogList.OnListItemClickListener;

public class WZMLInfoActivity extends BaseProcessPhotoActivity {
	ListView lv;
	private WZMLInfoBean mWZMLInfo;
	private Spinner spinnerendemismWZML;
	private Spinner spinnercharactersWZML;
	private Spinner spinnerdegreesWZML;
	private Spinner spinnerthreatStatesWZML;
	private Spinner spinnerdistributionTypeWZML;
	private Spinner spinnerlevelWZML;
	private Spinner spinnernicheWZML;
	private Spinner spinnerAreaWZML;

	private EditText etchineseNameWZML;
	private EditText etlatinNameWZML;
	private EditText etCommonNameWZML;
	private EditText etelevationBeginWZML;
	private EditText etelevationEndWZML;
	private EditText etenvironmentFeatureWZML;
	private EditText etdistributionAreaWZML;

	// 受威胁因素
	private CheckBox checkSZJHWZ;// 生境退化
	private CheckBox checkGDWJWZ;// 过度采掘
	private CheckBox checkZRZHJWZ;// 自然灾害
	private CheckBox checkWZYXWZ;// 本地物种影响
	private CheckBox checkHJWRWZ;// 环境污染
	private CheckBox checkRWFRWZ;// 其它人为干扰
	private CheckBox checkNZYSWZ;// 内在因素
	private CheckBox checkWLRQWZ;// 未来物种入侵影响
	private CheckBox checkOtherWZ;// 其它

	// 用途
	private CheckBox checkCyUse;// 材用
	private CheckBox checkYyUse;// 药用
	private CheckBox checkYllhUse;// 园林绿化
	private CheckBox checkSyUse;// 食用
	private CheckBox checkRzrlUse;// 鞣质与染料
	private CheckBox checkYlUse;// 油料
	private CheckBox checkXlUse;// 香料
	private CheckBox checkMyUse;// 蜜源
	private CheckBox checkXwUse;// 纤维
	private CheckBox checkOtherUse;// 其它

	private int mPosition;
	private CheckBox checkMZYyWZML;
	private CheckBox checkMZSyWZML;
	private CheckBox checkMZZywhWZML;
	private CheckBox checkMZOtherWZML;
	protected List<String> names;
	// private String title;
	// private String author;
	private List<LocalSpeciesBean> species;
	private Spinner spinnersaveGradeInCountryWZML;
	private Spinner spinnersaveGradeInProvinceWZML;
	private Spinner spinnerCITESWZML;
	private Spinner spinnerRedWZML;

	private RadioGroup radioGroupSave;
	private RadioGroup radioGroupOrigin;
	protected int mSaveStates;
	private RadioButton radioSaveQN;
	private RadioButton radioSaveQD;
	private RadioButton radioSaveNo;
	protected int mIsOrigin;
	private RadioButton radioYes;
	private RadioButton radioNo;
	private TextView mTVCurrentPage;
	private View view;
	private Animation mAnimationLeftIn;
	private Animation mAnimationRightIn;

	@Override
	public void initUI() {
		mWZMLInfo = (WZMLInfoBean) this.getIntent().getSerializableExtra(
				"WZMLInfo");
		mWhich = this.getIntent().getIntExtra("which", 0);
		mPosition = this.getIntent().getIntExtra("position", 0);

		switch (mWhich) {
		case 0:// 查看
			setContentView(R.layout.wzml_info_see);
			 this.findViewById(R.id.layoutButton).setVisibility(View.VISIBLE);
			break;
		case 1:// 添加
			setContentView(R.layout.wzml_info);
			this.findViewById(R.id.layoutButton).setVisibility(View.VISIBLE);
			this.findViewById(R.id.btnDelYF).setVisibility(View.GONE);
			break;
		case 3:// 修改
			setContentView(R.layout.wzml_info);
			this.findViewById(R.id.layoutButton).setVisibility(View.VISIBLE);
			break;
		default:
			setContentView(R.layout.wzml_info);
			this.findViewById(R.id.layoutButton).setVisibility(View.VISIBLE);
			this.findViewById(R.id.btnDelYF).setVisibility(View.GONE);
			break;
		}

		if (GlobalVars.getInst().latitude == 0.0) {
			initMapLocation();
		}

		layoutCameraYF = (LinearLayout) this.findViewById(R.id.layoutCameraYF);

		this.findViewById(R.id.btnCameraYF).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(WZMLInfoActivity.this,
								CameraActivity.class);
						String imgName = getImgName(true);
						String imgPath = getImgPath(true);
						intent.putExtra("imgPath", imgPath);
						intent.putExtra("imgName", imgName);
						intent.putExtra("title", imgTitle);
						intent.putExtra("author", imgAuthor);
						startActivityForResult(intent, 1);
					}
				});

		layoutVedioYF = (LinearLayout) this.findViewById(R.id.layoutVedioYF);

		this.findViewById(R.id.btnVedioYF).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(WZMLInfoActivity.this,
								VideoActivity.class);
						String imgName = getImgName(false);
						String imgPath = getImgPath(false);
						intent.putExtra("imgPath", imgPath);
						intent.putExtra("imgName", imgName);
						intent.putExtra("title", videoTitle);
						intent.putExtra("author", videoAuthor);
						startActivityForResult(intent, 2);
					}
				});

		spinnerAreaWZML = (Spinner) this.findViewById(R.id.spinnerAreaWZML);
		initSpinner(spinnerAreaWZML, GlobalVars.getInst().getArea());

		spinnercharactersWZML = (Spinner) this
				.findViewById(R.id.spinnercharactersWZML);
		initSpinner(spinnercharactersWZML, GlobalVars.getInst().wzmlCharacters);

		spinnerendemismWZML = (Spinner) this
				.findViewById(R.id.spinnerendemismWZML);
		initSpinner(spinnerendemismWZML, GlobalVars.getInst().wzmlEndemism);

		spinnerdegreesWZML = (Spinner) this
				.findViewById(R.id.spinnerdegreesWZML);
		initSpinner(spinnerdegreesWZML, GlobalVars.getInst().wzmlDegrees);

		spinnerthreatStatesWZML = (Spinner) this
				.findViewById(R.id.spinnerthreatStatesWZML);
		initSpinner(spinnerthreatStatesWZML,
				GlobalVars.getInst().wzmlThreatStates);

		spinnerdistributionTypeWZML = (Spinner) this
				.findViewById(R.id.spinnerdistributionTypeWZML);
		initSpinner(spinnerdistributionTypeWZML,
				GlobalVars.getInst().wzmlFormations);

		spinnerlevelWZML = (Spinner) this.findViewById(R.id.spinnerlevelWZML);
		initSpinner(spinnerlevelWZML, GlobalVars.getInst().wzmlLevels);

		spinnernicheWZML = (Spinner) this.findViewById(R.id.spinnernicheWZML);
		initSpinner(spinnernicheWZML, GlobalVars.getInst().wzmlNiches);
		
		spinnersaveGradeInCountryWZML = (Spinner) this
				.findViewById(R.id.spinnersaveGradeInCountryWZML);
		initSpinner(spinnersaveGradeInCountryWZML,
				GlobalVars.getInst().wzmlCountryGrade);

		spinnersaveGradeInProvinceWZML = (Spinner) this
				.findViewById(R.id.spinnersaveGradeInProvinceWZML);
		initSpinner(spinnersaveGradeInProvinceWZML,
				GlobalVars.getInst().wzmlPrivinceGrade);

		spinnerCITESWZML = (Spinner) this.findViewById(R.id.spinnerCITESWZML);
		initSpinner(spinnerCITESWZML, GlobalVars.getInst().wzmlCITESGrade);

		spinnerRedWZML = (Spinner) this.findViewById(R.id.spinnerRedWZML);
		initSpinner(spinnerRedWZML, GlobalVars.getInst().wzmlRedGrade);

		etchineseNameWZML = (EditText) this
				.findViewById(R.id.etchineseNameWZML);
		etlatinNameWZML = (EditText) this.findViewById(R.id.etlatinNameWZML);
		etCommonNameWZML = (EditText) this.findViewById(R.id.etCommonNameWZML);

		etelevationBeginWZML = (EditText) this
				.findViewById(R.id.etelevationBeginWZML);
		
		
		

		etelevationEndWZML = (EditText) this
				.findViewById(R.id.etelevationEndWZML);
		

		etenvironmentFeatureWZML = (EditText) this
				.findViewById(R.id.etenvironmentFeatureWZML);
		

		etdistributionAreaWZML = (EditText) this
				.findViewById(R.id.etdistributionAreaWZML);
		

		checkSZJHWZ = (CheckBox) this.findViewById(R.id.checkSZJHWZML);
		checkGDWJWZ = (CheckBox) this.findViewById(R.id.checkGDWJWZML);
		checkZRZHJWZ = (CheckBox) this.findViewById(R.id.checkZRZHJWZML);
		checkWZYXWZ = (CheckBox) this.findViewById(R.id.checkWZYXWZML);
		checkHJWRWZ = (CheckBox) this.findViewById(R.id.checkHJWRWZML);
		checkRWFRWZ = (CheckBox) this.findViewById(R.id.checkRWFRWZML);
		checkNZYSWZ = (CheckBox) this.findViewById(R.id.checkNZYSWZML);
		checkWLRQWZ = (CheckBox) this.findViewById(R.id.checkWLRQWZML);
		checkOtherWZ = (CheckBox) this.findViewById(R.id.checkOtherWZML);

		checkCyUse = (CheckBox) this.findViewById(R.id.checkCyWZML);
		checkYyUse = (CheckBox) this.findViewById(R.id.checkYyWZML);
		checkYllhUse = (CheckBox) this.findViewById(R.id.checkYllhJWZML);
		checkSyUse = (CheckBox) this.findViewById(R.id.checkSyWZML);
		checkRzrlUse = (CheckBox) this.findViewById(R.id.checkRzrlWZML);
		checkYlUse = (CheckBox) this.findViewById(R.id.checkYlWZML);
		checkXlUse = (CheckBox) this.findViewById(R.id.checkXlWZML);
		checkMyUse = (CheckBox) this.findViewById(R.id.checkMyWZML);
		checkXwUse = (CheckBox) this.findViewById(R.id.checkXwWZML);
		checkOtherUse = (CheckBox) this.findViewById(R.id.checkOtherYtWZML);

		checkMZYyWZML = (CheckBox) this.findViewById(R.id.checkMZYyWZML);
		checkMZSyWZML = (CheckBox) this.findViewById(R.id.checkMZSyWZML);
		checkMZZywhWZML = (CheckBox) this.findViewById(R.id.checkMZZywhWZML);
		checkMZOtherWZML = (CheckBox) this.findViewById(R.id.checkMZOtherWZML);

		radioGroupSave = (RadioGroup) this.findViewById(R.id.radioGroupSave);
		radioGroupSave
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.radioSaveQN:
							mSaveStates = 0;
							break;
						case R.id.radioSaveQD:
							mSaveStates = 1;
							break;
						case R.id.radioSaveNo:
							mSaveStates = 2;
							break;
						default:
							break;
						}

					}
				});
		radioSaveQN = (RadioButton) this.findViewById(R.id.radioSaveQN);
		radioSaveQD = (RadioButton) this.findViewById(R.id.radioSaveQD);
		radioSaveNo = (RadioButton) this.findViewById(R.id.radioSaveNo);

		radioGroupOrigin = (RadioGroup) this
				.findViewById(R.id.radioGroupOrigin);
		radioGroupOrigin
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.radioYes:
							mIsOrigin = 0;
							break;
						case R.id.radioNo:
							mIsOrigin = 1;
							break;
						default:
							break;
						}
					}
				});
		radioYes = (RadioButton) this.findViewById(R.id.radioYes);
		radioNo = (RadioButton) this.findViewById(R.id.radioNo);

		if (mWhich == 0) {
			view = this.findViewById(R.id.scroller);
			view.setOnTouchListener(this);
			mAnimationLeftIn = AnimationUtils.loadAnimation(this,
					R.anim.slide_left_in);
			mAnimationRightIn = AnimationUtils.loadAnimation(this,
					R.anim.slide_right_in);
			modifyEditStyle();
			mTVCurrentPage = (TextView)this.findViewById(R.id.currentPage);
			updatePage();
		}

	}
	
	public void updatePage() {
		if(mTVCurrentPage != null) {
			mTVCurrentPage.setText((mPosition + 1) + "/" + WZMLInfoTableActivity.mWZMLInfos.size());
		}
		
	}

	public void fillData() {
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.imgPath)) {
			String[] strImgInfo = mWZMLInfo.imgPath.split(":");
			sbImgUrl = new StringBuffer("");
			sbImgName = new StringBuffer("");
			
			sbImgUrl.append(strImgInfo[0]);
			sbImgName.append(strImgInfo[1]);
			fillBitmapToLayout(sbImgUrl.toString(),sbImgName.toString(), layoutCameraYF, true);
		}else {
			layoutCameraYF.removeAllViews();
		}

		if (mWZMLInfo != null) {
			imgTitle = mWZMLInfo.imgTitle;
			imgAuthor = mWZMLInfo.imgAuthor;
			videoTitle = mWZMLInfo.videoTitle;
			videoAuthor = mWZMLInfo.videoAuthor;
		}

		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.videoPath)) {
			String[] strVideoInfo = mWZMLInfo.videoPath.split(":");
			sbVideoUrl = new StringBuffer("");
			sbVideoName = new StringBuffer("");
			
			sbVideoUrl.append(strVideoInfo[0]);
			sbVideoName.append(strVideoInfo[1]);
			fillBitmapToLayout(sbVideoUrl.toString(),sbVideoName.toString(), layoutVedioYF, false);
		}else {
			layoutVedioYF.removeAllViews();
		}
		if (mWZMLInfo != null) {
			spinnerAreaWZML.setSelection(mWZMLInfo.surveyAreaPosition);
		}else {
			spinnerAreaWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnercharactersWZML.setSelection(mWZMLInfo.characters);
		}else {
			spinnercharactersWZML.setSelection(0);
		}
		
		if (mWZMLInfo != null
				&& !StringUtils.isEmpty(mWZMLInfo.distributionArea)) {
			etdistributionAreaWZML.setText(mWZMLInfo.distributionArea);
		}else {
			etdistributionAreaWZML.setText("");
		}
		if (mWZMLInfo != null
				&& !StringUtils.isEmpty(mWZMLInfo.environmentFeature)) {
			etenvironmentFeatureWZML.setText(mWZMLInfo.environmentFeature);
		}else {
			etenvironmentFeatureWZML.setText("");
		}
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.elevationEnd)) {
			etelevationEndWZML.setText(mWZMLInfo.elevationEnd);
		} else {
			etelevationEndWZML.setText(GlobalVars.getInst().altitude + "");
		}
		
		
		if (mWZMLInfo != null) {
			spinnerendemismWZML.setSelection(mWZMLInfo.endemism);
		}else {
			spinnerendemismWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnerdegreesWZML.setSelection(mWZMLInfo.degrees);
		}else {
			spinnerdegreesWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnerthreatStatesWZML.setSelection(mWZMLInfo.threatStates);
		}else {
			spinnerthreatStatesWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnerdistributionTypeWZML
			.setSelection(mWZMLInfo.distributionTypePosition);
		}else {
			spinnerdistributionTypeWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnernicheWZML.setSelection(mWZMLInfo.niche);
		}else {
			spinnernicheWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnerlevelWZML.setSelection(mWZMLInfo.level);
		}else {
			spinnerlevelWZML.setSelection(0);
		}

		
		if (mWZMLInfo != null) {
			spinnersaveGradeInCountryWZML
			.setSelection(mWZMLInfo.saveGradeInCountry);
		}else {
			spinnersaveGradeInCountryWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnersaveGradeInProvinceWZML
			.setSelection(mWZMLInfo.saveGradeInPrivince);
		}else {
			spinnersaveGradeInProvinceWZML.setSelection(0);
		}
		if (mWZMLInfo != null) {
			spinnerCITESWZML.setSelection(mWZMLInfo.saveGradeInCITES);
		}else {
			spinnerCITESWZML.setSelection(0);
		}
		
		if (mWZMLInfo != null) {
			spinnerRedWZML.setSelection(mWZMLInfo.saveGradeInRed);
		}else {
			spinnerRedWZML.setSelection(0);
		}
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.chineseName)) {
			etchineseNameWZML.setText(mWZMLInfo.chineseName);
		}else {
			etchineseNameWZML.setText("");
		}
		
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.latinName)) {
			etlatinNameWZML.setText(mWZMLInfo.latinName);
		}else {
			etlatinNameWZML.setText("");
		}
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.commonName)) {
			etCommonNameWZML.setText(mWZMLInfo.commonName);
		}else {
			etCommonNameWZML.setText("");
		}
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.elevationBegin)) {
			etelevationBeginWZML.setText(mWZMLInfo.elevationBegin);
		} else {
			etelevationBeginWZML.setText(GlobalVars.getInst().altitude + "");
		}
		if (mWZMLInfo != null) {
			switch (mWZMLInfo.isOrigin) {
			case 0:
				radioYes.setChecked(true);
				break;
			case 1:
				radioNo.setChecked(true);
				break;
			default:
				break;
			}

		}

		if (mWZMLInfo != null) {
			switch (mWZMLInfo.saveStates) {
			case 0:
				radioSaveQN.setChecked(true);
				break;
			case 1:
				radioSaveQD.setChecked(true);
				break;
			case 2:
				radioSaveNo.setChecked(true);
				break;

			default:
				break;
			}

		}

		// 用途
		boolean[] isChecksNationUse = { false, false, false, false };
		CheckBox[] checksNationUse = { checkMZYyWZML, checkMZSyWZML,
				checkMZZywhWZML, checkMZOtherWZML };
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.nationalUse)) {
			String[] typesNationUse = mWZMLInfo.nationalUse.split(",");
			for (int i = 0; i < typesNationUse.length; i++) {
				if ((typesNationUse[i]).equals("1")) {
					isChecksNationUse[i] = true;
				} else {
					isChecksNationUse[i] = false;
				}
				checksNationUse[i].setChecked(isChecksNationUse[i]);
			}

		}

		// 用途
		boolean[] isChecksUse = { false, false, false, false, false, false,
				false, false, false, false };
		CheckBox[] checksUse = { checkCyUse, checkYyUse, checkYllhUse,
				checkSyUse, checkRzrlUse, checkYlUse, checkXlUse, checkMyUse,
				checkXwUse, checkOtherUse };
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.use)) {
			String[] typesUse = mWZMLInfo.use.split(",");
			for (int i = 0; i < typesUse.length; i++) {
				if ((typesUse[i]).equals("1")) {
					isChecksUse[i] = true;
				} else {
					isChecksUse[i] = false;
				}
				checksUse[i].setChecked(isChecksUse[i]);
			}

		}

		// 受威胁因素
		boolean[] isChecksThreadFactors = { false, false, false, false, false,
				false, false, false, false };
		CheckBox[] checksThreadFactors = { checkSZJHWZ, checkGDWJWZ,
				checkZRZHJWZ, checkWZYXWZ, checkHJWRWZ, checkRWFRWZ,
				checkNZYSWZ, checkWLRQWZ, checkOtherWZ };
		if (mWZMLInfo != null && !StringUtils.isEmpty(mWZMLInfo.threatFactors)) {
			String[] typesThreadFactors = mWZMLInfo.threatFactors.split(",");
			for (int i = 0; i < typesThreadFactors.length; i++) {
				if ((typesThreadFactors[i]).equals("1")) {
					isChecksThreadFactors[i] = true;
				} else {
					isChecksThreadFactors[i] = false;
				}
				checksThreadFactors[i].setChecked(isChecksThreadFactors[i]);
			}

		}

	}

	@Override
	public void initData() {
		fillData();
		viewOnClick();
	}

	private void viewOnClick() {
		this.findViewById(R.id.btnSaveYF).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						if (mWhich == 0) {
							prePage();
							return;
						}

						String latinName = etlatinNameWZML.getText().toString();
						if (StringUtils.isEmpty(latinName)) {
							showToast("您还没有填写拉丁名");
							return;
						}
						String chineseName = etchineseNameWZML.getText()
								.toString();
						if (StringUtils.isEmpty(chineseName)) {
							showToast("您还没有填写中文名");
							return;
						}

						WZMLInfoBean wzmlInfo = createWZMLInfo();
						if (wzmlInfo != null) {

							Intent intent = new Intent();
							if (mWhich == 1) {
								WZMLInfoDB.insert(wzmlInfo);
							} else if (mWhich == 3) {
								WZMLInfoDB.updateById(wzmlInfo, mWZMLInfo.id);
								intent.putExtra("update", wzmlInfo);
								intent.putExtra("position", mPosition);
							} else {
								WZMLInfoDB.insert(wzmlInfo);
							}
							showToast("保存成功");
							setResult(RESULT_OK, intent);
							finish();
						}

					}
				});

		this.findViewById(R.id.btnDelYF).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (mWhich == 0) {
							nextPage();
							return;
						}
						new AlertDialog.Builder(WZMLInfoActivity.this)
						.setMessage("确认删除吗?")
						.setPositiveButton("确定", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								WZMLInfoDB.delete(mWZMLInfo);
								showToast("删除成功");
								Intent intent = new Intent();
								intent.putExtra("delete", mWZMLInfo);
								intent.putExtra("position", mPosition);
								setResult(RESULT_OK, intent);
								finish();
							}
						}).setNegativeButton("取消", null).show();

					}
				});

		this.findViewById(R.id.btnBackWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});

		this.findViewById(R.id.btnQuaryChineseNameWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						String chinese = etchineseNameWZML.getText().toString();
						showDialog(true, chinese);
					}
				});
		this.findViewById(R.id.btnQuaryLatinNameWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						String latin = etlatinNameWZML.getText().toString();
						showDialog(false, latin);
					}
				});
		this.findViewById(R.id.btnLocateWZML).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						startLocation();
					}
				});

	}

	protected void prePage() {
		if (mPosition == 0) {
			showToast("没有更多物种名录了.");
			return;
		}
		view.startAnimation(mAnimationRightIn);
		mWZMLInfo = null;
		mWZMLInfo = WZMLInfoTableActivity.mWZMLInfos
				.get(mPosition - 1);
		mPosition--;
		fillData();
		updatePage();
	}

	/**
	 * 上一页
	 */
	protected void nextPage() {
		if (mPosition == WZMLInfoTableActivity.mWZMLInfos
				.size() - 1) {
			showToast("没有更多物种名录了.");
			return;
		}
		view.startAnimation(mAnimationLeftIn);
		mWZMLInfo = null;
		mWZMLInfo = WZMLInfoTableActivity.mWZMLInfos
				.get(mPosition + 1);
		mPosition++;
		fillData();
		updatePage();
	}
	
	@Override
	protected void ProcessPrePage() {
		prePage();
	}
	
	@Override
	protected void ProcessNextPage() {
		nextPage();
	}

	public void showDialog(boolean isChenese, String queryContent) {
		names = new ArrayList<String>();
		if (isChenese) {
			species = LocalSpeciesInfoDB.query("", queryContent);
		} else {
			species = LocalSpeciesInfoDB.query(queryContent, "");
		}
		if (species != null) {
			for (int i = 0; i < species.size(); i++) {
				if (isChenese) {
					names.add(species.get(i).Chinese);
				} else {
					names.add(species.get(i).SName);
				}

			}

		}
		if (names != null && names.size() != 0) {
			CommonDialogList namesDialog = new CommonDialogList(_activity);
			namesDialog.setItems(names, new OnListItemClickListener() {

				@Override
				public void onListItemClick(int position) {
					etchineseNameWZML.setText(species.get(position).Chinese);
					etlatinNameWZML.setText(species.get(position).SName);
				}
			});
			namesDialog.show();

		} else {
			if (isChenese) {
				_activity.showToast("您搜索的中文名不存在");
			} else {
				_activity.showToast("您搜索的中文名不存在");
			}
		}
	}

	public WZMLInfoBean createWZMLInfo() {
		WZMLInfoBean wzmlInfo = new WZMLInfoBean();
		wzmlInfo.groupId = "";
		wzmlInfo.surveyMethod = "物种名录";
		wzmlInfo.surveyArea = spinnerAreaWZML.getSelectedItem().toString();
		wzmlInfo.surveyAreaPosition = spinnerAreaWZML.getSelectedItemPosition();
		wzmlInfo.chineseName = etchineseNameWZML.getText().toString();
		wzmlInfo.latinName = etlatinNameWZML.getText().toString();
		wzmlInfo.commonName = etCommonNameWZML.getText().toString();
		wzmlInfo.characters = spinnercharactersWZML.getSelectedItemPosition();
		wzmlInfo.distributionTypePosition = spinnerdistributionTypeWZML
				.getSelectedItemPosition();
		wzmlInfo.distributionArea = etdistributionAreaWZML.getText().toString();
		wzmlInfo.environmentFeature = etenvironmentFeatureWZML.getText()
				.toString();
		wzmlInfo.elevationBegin = etelevationBeginWZML.getText().toString();
		wzmlInfo.elevationEnd = etelevationEndWZML.getText().toString();
		wzmlInfo.level = spinnerlevelWZML.getSelectedItemPosition();
		wzmlInfo.niche = spinnernicheWZML.getSelectedItemPosition();
		wzmlInfo.endemism = spinnerendemismWZML.getSelectedItemPosition();
		wzmlInfo.degrees = spinnerdegreesWZML.getSelectedItemPosition();
		wzmlInfo.threatStates = spinnerthreatStatesWZML
				.getSelectedItemPosition();

		wzmlInfo.saveGradeInCountry = spinnersaveGradeInCountryWZML
				.getSelectedItemPosition();
		wzmlInfo.saveGradeInPrivince = spinnersaveGradeInProvinceWZML
				.getSelectedItemPosition();
		wzmlInfo.saveGradeInCITES = spinnerCITESWZML.getSelectedItemPosition();
		wzmlInfo.saveGradeInRed = spinnerRedWZML.getSelectedItemPosition();

		wzmlInfo.threatFactors = (checkSZJHWZ.isChecked() ? "1" : "0") + ","
				+ (checkGDWJWZ.isChecked() ? "1" : "0") + ","
				+ (checkZRZHJWZ.isChecked() ? "1" : "0") + ","
				+ (checkWZYXWZ.isChecked() ? "1" : "0") + ","
				+ (checkHJWRWZ.isChecked() ? "1" : "0") + ","
				+ (checkRWFRWZ.isChecked() ? "1" : "0") + ","
				+ (checkNZYSWZ.isChecked() ? "1" : "0") + ","
				+ (checkWLRQWZ.isChecked() ? "1" : "0") + ","
				+ (checkOtherWZ.isChecked() ? "1" : "0");

		wzmlInfo.use = (checkCyUse.isChecked() ? "1" : "0") + ","
				+ (checkYyUse.isChecked() ? "1" : "0") + ","
				+ (checkYllhUse.isChecked() ? "1" : "0") + ","
				+ (checkSyUse.isChecked() ? "1" : "0") + ","
				+ (checkRzrlUse.isChecked() ? "1" : "0") + ","
				+ (checkYlUse.isChecked() ? "1" : "0") + ","
				+ (checkXlUse.isChecked() ? "1" : "0") + ","
				+ (checkMyUse.isChecked() ? "1" : "0") + ","
				+ (checkXwUse.isChecked() ? "1" : "0") + ","
				+ (checkOtherUse.isChecked() ? "1" : "0");

		wzmlInfo.nationalUse = (checkMZYyWZML.isChecked() ? "1" : "0") + ","
				+ (checkMZSyWZML.isChecked() ? "1" : "0") + ","
				+ (checkMZZywhWZML.isChecked() ? "1" : "0") + ","
				+ (checkMZOtherWZML.isChecked() ? "1" : "0");

		wzmlInfo.isOrigin = mIsOrigin;
		wzmlInfo.saveStates = mSaveStates;

		String imgName = getImgName(true);
		String imgPath = getImgPath(true);
		if (imgPath != null && !"".equals(imgPath.toString())) {
			wzmlInfo.imgPath = imgPath + ":" +  imgName;
		}
		wzmlInfo.imgTitle = imgTitle;
		wzmlInfo.imgAuthor = imgAuthor;

		String vedioName = getImgName(false);
		String vedioPath = getImgPath(false);
		if (vedioPath != null && !"".equals(vedioPath.toString())) {
			wzmlInfo.videoPath = vedioPath + ":" +  vedioName;
		}

		wzmlInfo.videoTitle = videoTitle;
		wzmlInfo.videoAuthor = videoAuthor;
		return wzmlInfo;
	}

	

	public void initSpinner(Spinner spinner, String[] data) {
		ArrayAdapter<String> areaAdapter = new ArrayAdapter<String>(this,
				R.layout.item_spinner, data);
		areaAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(areaAdapter);
	}

	@Override
	public void setLocateInfo() {
		if (etelevationBeginWZML != null && etelevationEndWZML != null) {
			etelevationBeginWZML.setText(GlobalVars.getInst().altitude + "");
			etelevationEndWZML.setText(GlobalVars.getInst().altitude + "");
		}
	}

	/**
	 * 当页面为查看样式时,更改文本框显示样式
	 */
	public void modifyEditStyle() {
		etchineseNameWZML.setBackgroundResource(0);
		etlatinNameWZML.setBackgroundResource(0);
		etCommonNameWZML.setBackgroundResource(0);
		etelevationBeginWZML.setBackgroundResource(0);
		etelevationEndWZML.setBackgroundResource(0);
		etenvironmentFeatureWZML.setBackgroundResource(0);
		etdistributionAreaWZML.setBackgroundResource(0);

		this.findViewById(R.id.btnLocateWZML).setVisibility(View.GONE);
		this.findViewById(R.id.btnQuaryChineseNameWZML)
				.setVisibility(View.INVISIBLE);
		this.findViewById(R.id.btnQuaryLatinNameWZML).setVisibility(View.INVISIBLE);

		this.findViewById(R.id.btnCameraYF).setVisibility(View.GONE);
		this.findViewById(R.id.btnVedioYF).setVisibility(View.GONE);

		spinnerendemismWZML.setBackgroundResource(0);
		spinnercharactersWZML.setBackgroundResource(0);
		spinnerdegreesWZML.setBackgroundResource(0);
		spinnerthreatStatesWZML.setBackgroundResource(0);
		spinnerdistributionTypeWZML.setBackgroundResource(0);
		spinnerlevelWZML.setBackgroundResource(0);
		spinnernicheWZML.setBackgroundResource(0);
		spinnerAreaWZML.setBackgroundResource(0);

		spinnersaveGradeInCountryWZML.setBackgroundResource(0);
		spinnersaveGradeInProvinceWZML.setBackgroundResource(0);
		spinnerCITESWZML.setBackgroundResource(0);
		spinnerRedWZML.setBackgroundResource(0);

	}

	@Override
	protected void onResume() {
		startLocation();
		super.onResume();
	}

	@Override
	protected void onPause() {
		stopLocation();
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		destroyBMapManager();
		super.onDestroy();
	}

}
